'use strict';
angular.module('sbAdminApp')
    .controller('SettlementresultCtrl', ['$scope', '$modal', '$log', '$location', 'settlementresultService', function ($scope, $modal, $log, $location, settlementresultService) {
        //初始化作用域变量
        $scope.lists = [];
        $scope.totalItems = 0;
        $scope.currentPage = 1;
        $scope.itempersize = 10;
   
        //切换分页事件时触发方法
        $scope.pageChanged = function () {
        	 $scope.search1();
        };
        
        //结算  
        $scope.statements = function () {
        	var tableElement = document.getElementById("table1");
      		var size = tableElement.rows.length;
      		var selectedIds = "";
      		for(var i =0;i<size-1;i++) {
      		var checkbox = document.getElementsByName("check")[i];
      			if(checkbox.checked==true){
      				selectedIds += checkbox.value + ",";
      			}
      		}
      		if(selectedIds==""){
      			alert("没有选择要结算的数据！");
      			return;
      		}else{
      			 selectedIds=selectedIds.substr(0, selectedIds.length-1);
          		 settlementresultService.statements(selectedIds).then(function (dataResponse) {
          			if(dataResponse.data['result']>0){
          				$scope.search1();
//          				alert("结算成功！");
          			}else{
          				alert("结算失败！");
          			}
                });
      		}
        };
        
        //点击查询触发
        $scope.search1 = function () {
        	settlementresultService.search1($scope.sresult,
            		$scope.currentPage, 
            		$scope.itempersize).then(function (dataResponse) {
            	$scope.lists = dataResponse.data['result'];
                $scope.totalItems = dataResponse.data['page'].totalRecord;
            });
        };
        
        
        //点击导出触发
        $scope.export1 = function (currentPage) {
        	if(currentPage==null || currentPage==undefined || !currentPage){
        		settlementresultService.export1($scope.sresult, null ,null,false);
        	}else{
        		settlementresultService.export1($scope.sresult,
                    	$scope.currentPage , 
                    	$scope.itempersize,
                    	true);
        	}
        	
        };
        
        
        //重新结算
        $scope.reSettlement = function (sresult) {
        	settlementresultService.reSettlement( sresult).then(function (dataResponse) {
                if(dataResponse.data['result']){
                	alert("重新结算成功！");
                	$scope.search1();
                	//重新加载数据
                }else{
                	alert("重新结算失败！");
                }
            });
        };
        
        
        //加载作用域数据
        $scope.search1();;

    }]);